package com.zhongke.DKYCboot.util;

/**
 * 创建人:zhangtiande 时间:2020/5/14 8:19
 */
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.stereotype.Service;
@Service
public class OssUtil {


          private static final String endpoint =  "https://oss-cn-zhangjiakou.aliyuncs.com";
          private static final String accessKeyId =  "LTAIO9AdgR3iRtMv";// 这里是你的授权KeyId
          private static final String accessKeySecret =  "6FIWSXXYQ4f8Bv4by0re2kXXVY18Zo";// 这里是你的授权秘钥
          private static final String bucketName = "shiyutang";
        /**
         * OSS的文件夹名
         */
        /*
         * private static String folder = "banner/";//这里是你存放图片的文件夹名
         */
       //  private static final String key = "https://haitunxiche.oss-cn-zhangjiakou.aliyuncs.com /";

        /**
         * 上传图片
         *
         * @param fileName    文件名
         * @param inputStream 流
         */
        public  String uploadImageToOSS(String fileName, InputStream inputStream, String folder) {
            /**
             * 创建OSS客户端
             */
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            try {
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String[] names = fileName.split("[.]");
                String name = uuid + "." + names[names.length - 1];
                ossClient.putObject(new PutObjectRequest(bucketName, folder + name, inputStream));
                return folder + name;
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(e.getMessage());
            } finally {
                ossClient.shutdown();
            }
            return null;
        }

        /**
         * 多图上传
         */
        public String[] uploadImageToOSS(String[] fileNames, InputStream[] inputStreams, String folder) {
            /**
             * 创建OSS客户端
             */
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            try {
                ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue(Arrays.asList(fileNames));
                ConcurrentLinkedQueue<InputStream> streamConcurrentLinkedQueue = new ConcurrentLinkedQueue(
                        Arrays.asList(inputStreams));
                Iterator<InputStream> inputStreamss = streamConcurrentLinkedQueue.iterator();
                ConcurrentLinkedQueue c = new ConcurrentLinkedQueue();
                for (Iterator<String> iterator = concurrentLinkedQueue.iterator(); iterator.hasNext()
                        && inputStreamss.hasNext();) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    String[] names = iterator.next().split("[.]");
                    String name = uuid + "." + names[names.length - 1];
                    ossClient.putObject(new PutObjectRequest(bucketName, folder + name, inputStreamss.next()));
                    c.add( folder + name);
                }
                return (String[]) c.toArray(new String[0]);
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(e.getMessage());
            } finally {
                ossClient.shutdown();
            }
            return null;
        }

        /**
         * 删除图片 警告：在没有调用其他方法的情况下，请调用closeClient方法
         *
         * @param url URL全路径
         */
        public void deleteImg(String url, String folder) {
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            if (url == null || "".equals(url)) {
                return;
            }
            String[] paths = url.split("[.]");
            /**
             * 文件夹是否存在
             */
            if (!ossClient.doesObjectExist(bucketName, folder)) {
                ossClient.putObject(bucketName, folder, new ByteArrayInputStream(new byte[0]));
            }
            String[] name = paths[paths.length - 2].split("[/]");
            /**
             * 对象是否存在
             */
            if (ossClient.doesObjectExist(bucketName, folder + name[name.length - 1] + "." + paths[paths.length - 1])) {
                /**
                 * 删除存在对象
                 */
                ossClient.deleteObject(bucketName, folder + name[name.length - 1] + "." + paths[paths.length - 1]);
            }
            ossClient.shutdown();
        }
}

